package servlet.product.user;

import bean.Page;
import bean.Product;
import service.ProductService;
import service.impl.ProductServiceImpl;
import service.impl.UserServiceImpl;
import utils.CookieUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/c.do")
public class ProductCreate extends HttpServlet {
    ProductService userService = new ProductServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        String pageNo = req.getParameter("pageNo");
        int pgn = 0;
        if (pageNo == null||Integer.parseInt(pageNo)<0) {
            pgn = 1;
        }else {
            pgn= Integer.parseInt(pageNo);
        }
        Page<Product> productPage = userService.queryByPage(5,pgn);
        ArrayList<Product> products = (ArrayList<Product>) productPage.getData();
        session.setAttribute("products", products);

        session.setAttribute("pgn",pgn);
        session.setAttribute("tpg",productPage.getPageCount());
        resp.sendRedirect(req.getContextPath() + "/user/productList.jsp");


    }
}
